<template>
    <div>
        <Spin fix v-if="ifSpin">
            <Icon type="ios-loading" size=18 class="demo-spin-icon-load"></Icon>
            <div>附件生成中</div>
        </Spin>
        <Form :label-width="90" ref="formInline" :model="formInline">
            <Row :gutter="16" class="query-form">
                <i-col span="8">
                    <FormItem label="排名日期：">
                        <DatePicker v-model="formInline.rankDate" style="width: 100%" format="yyyy-MM-dd" placeholder="请选择排名时间"></DatePicker>
                    </FormItem>
                </i-col>
                <i-col span="16">
                    <div style="float: right;">
                        <Button class="ivu-btn-reset" @click="cancel">重置</Button>
                        <Button class="ivu-btn-query" icon="md-search"  @click="query">查询</Button>
                    </div>
                </i-col>
            </Row>
        </Form>
        <Row type="flex" justify="start" class="query-form">
            <i-col>
                <Button icon="md-cloud-download" class="ivu-btn-add" @click="exportExcel">导出</Button>
            </i-col>
        </Row>
        <Table ref='table' style="margin-top: 10px" :loading="loading" border :columns="columns" no-data-text="没有查询到相关数据！"
               :data="infos" width="100%" :height="tableHeight" highlight-row stripe size="small"></Table>
    </div>
</template>

<script>

    import {Api} from '../../../api'

    export default {
        name: "assessmentOrgRank",
        data(){
            return{
                ifSpin: false,
                formInline:{},
                tableHeight: '',
                loading: false,
                columns: [
                    {
                        type: 'index',
                        title: '序号',
                        width: 80,
                        align: 'center',
                    },
                    {
                        title: '单位名称',
                        key: 'orgName',
                        align: 'center',
                    },
                    {
                        title: '分数',
                        key: 'source',
                        align: 'center',
                    },
                    {
                        type: 'index',
                        title: '排名',
                        width: 120,
                        align: 'center',
                    },
                ],
                infos: []
            }
        },
        mounted(){
            this.tableHeight = window.innerHeight - this.$refs.table.$el.offsetTop - 105;
            this.cancel();
        },
        methods: {
            exportExcel(){
                this.ifSpin = true;
                this.$http.get(Api.apsgAssessmentAbnormalRank + '/excel', {params: this.formInline, responseType: 'arraybuffer'}).then((res) => {
                    const aLink = document.createElement("a");
                    let blob = new Blob([res.data], {
                        type: "application/vnd.ms-excel"
                    });
                    aLink.href = URL.createObjectURL(blob);
                    aLink.download = "单位得分排名.xlsx" //标题
                    aLink.click();
                    document.body.appendChild(aLink);
                    this.ifSpin = false;
                }).catch(e => {
                    this.$Message.error("数据获取失败")
                })
            },
            query(){
                this.loading = true;
                if(this.formInline.rankDate != null){
                    this.formInline.rankDate = this.formatDate(this.formInline.rankDate, 'yyyy-MM-dd');
                }else{
                    this.$Message.error("请选择排名日期");
                    return;
                }
                this.$http.get(Api.apsgAssessmentAbnormalRank, {params: this.formInline}).then((res) => {
                    this.infos = res.data.data;
                    this.loading = false;
                }).catch(e => {
                    this.$Message.error("数据获取失败")
                })
            },
            cancel(){
                this.formInline.rankDate = new Date();
                this.query();
            },
            formatDate(date, fmt) {
                if (typeof date == 'string') {
                    return date;
                }
                if (!fmt) fmt = "yyyy-MM-dd hh:mm:ss";
                if (!date || date == null) return null;
                let o = {
                    'M+': date.getMonth() + 1, // 月份
                    'd+': date.getDate(), // 日
                    'h+': date.getHours(), // 小时
                    'm+': date.getMinutes(), // 分
                    's+': date.getSeconds(), // 秒
                    'q+': Math.floor((date.getMonth() + 3) / 3), // 季度
                    'S': date.getMilliseconds() // 毫秒
                }
                if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
                for (let k in o) {
                    if (new RegExp('(' + k + ')').test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)))
                }
                return fmt
            },
        }
    }
</script>

<style scoped>
    .demo-spin-icon-load{
        animation: ani-demo-spin 1s linear infinite;
    }
</style>
